package com.hoshino.momochi.service.Impl;

import com.hoshino.momochi.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Service
//@Slf4j
public class MailServiceImpl implements MailService {
    @Value("${spring.mail.username}")
    private String from;

    @Value("${spring.mail.password}")
    private String password;

    @Override
    @Async
    public void sendMail(String toPerson, String code) {
//        log.info("------------mail-begin------------");
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

        //设定mail server
        senderImpl.setHost("smtp.qq.com");

        MimeMessage mailMessage = senderImpl.createMimeMessage();

        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8");
            //设置收件人，寄件人
            messageHelper.setTo(toPerson);
            messageHelper.setFrom(from);
            messageHelper.setSubject("验证码!！");
            //true 表示启动HTML格式的邮件
            messageHelper.setText("<div style=\"background: #F4F4F4;padding:10px;margin:-5px; min-height: 520px; text-align: center;\"><div style=\"margin: 0 auto; padding:0;display: inline-block;\"><div dir=\"ltr\" style=\"display: table-cell; vertical-align: middle; height: 520px; padding: 0; margin: 0;\"><table style=\"background: rgb(255, 255, 255); border-radius: 4px; width: 100%; max-width: 600px;\" align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\"><tbody><tr style=\"height:84px;\"><td style=\"width:24px; \"></td><td colspan=\"2\" style=\"border-bottom: 1px solid #F2F2F2;\"><img style=\"margin-top: 10px;\" src=\"https://webcnstatic.yostar.net/sdk/sdk_logo/yostar_email.png\" title=\"\"></td><td style=\"width: 24px;\"></td></tr><tr><td style=\"width: 24px;\"></td><td colspan=\"2\" style=\"padding-top:32px;padding-bottom: 24px;border-bottom: 1px solid #F2F2F2;\"><p style=\" font-size: 16px; font-weight: 600;color: #000000; line-height: 24px;margin: 0;padding: 0;text-align: left\">Hello，亲爱的用户<br> 完成 Hoshino 验证，请使用验证码：</p><div style=\"height: 96px;margin-top: 20px; background: #FFDD1F;border-radius: 4px;line-height: 96px;text-align: center;font-size: 32px;font-weight: 900;color: #000000;\">" + code + "</div><p style=\"margin: 0; font-size: 12px;color: #626262;line-height: 17px; margin-top: 20px;text-align: left\">该验证码有效期为 10分钟，且只能使用一次。如果请求不是您提出的，请忽略此电子邮件。该消息是自动发送的，无需进行回复。</p></td><td style=\"width: 24px;\"></td></tr><tr><td style=\"width:24px; \"></td><td style=\"padding-top:24px ;padding-bottom:24px\"><p style=\"margin: 0; text-align: left; font-size: 12px;font-weight:400;color: #858585; line-height: 17px;\"></p></td><td><p style=\"margin: 0; text-align: right;font-size: 12px;font-weight:400;color: #858585; line-height: 17px;\">Takanashi Hoshino</p></td><td style=\"width:24px; \"></td></tr></tbody></table></div></div></div>", true);
            senderImpl.setUsername(from);
            senderImpl.setPassword(password);
            Properties prop = new Properties();
            prop.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
            prop.put("mail.smtp.timeout", "25000");
            senderImpl.setJavaMailProperties(prop);
            senderImpl.send(mailMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
//        log.info("------------mail-end------------");
    }
}
